package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/setmeal")
@Api(tags = "套餐相关接口")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private DishService dishService;
    @Autowired
    private CategoryService categoryService;
    @PostMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    @ApiOperation(value = "新增套餐接口")
    public R<String>save(@RequestBody SetmealDto setmealDto){
        setmealService.saveWithDish(setmealDto);
        return R.success("新增套餐成功");
    }
    @GetMapping("/page")
    @ApiOperation(value = "套餐分页查询接口")
    public R<Page<SetmealDto>>page(Integer page,Integer pageSize,String name){
        Page<SetmealDto> dtoPage = setmealService.selectByPage(page, pageSize, name);
        return R.success(dtoPage);
    }
    @DeleteMapping
    @CacheEvict(value = "setmeal",allEntries = true)
    @ApiOperation(value = "套餐删除接口")
    public R<String>delete(Long[]ids){
        setmealService.removeWithDish(ids);
        return R.success("删除套餐成功");
    }

@GetMapping("/list")
@Cacheable(value = "setmeal",key = "#categoryId",unless = "#result.data.size()==0")
@ApiOperation(value = "套餐条件查询接口")
public R<List<Setmeal>>list(Long categoryId,Integer status){
    QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("category_id",categoryId);
    queryWrapper.eq("status",status);
    List<Setmeal> list = setmealService.list(queryWrapper);
    return R.success(list);
}
@GetMapping("/dish/{setmealId}")
public R<List<SetmealDishDto>> dish(@PathVariable Long setmealId){
    List<SetmealDishDto> dishDtos = setmealService.listWithImage(setmealId);
    return R.success(dishDtos);
    }
}
